#include <iostream>
using namespace std;

void sortArray(int *arr, int arraySize)
{
    for (int i = 0; i < arraySize - 1; i++)
    {
        for (int j = 0; j < arraySize - i - 1; j++)
        {
            int temp = 0;
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

void fillArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            arr[i][j] = rand() % 100 + 1;
        }
    }
}

void printArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

void spiralPrint(int **arr, int rows, int cols)
{
    int top = 0, bottom = rows - 1, left = 0, right = rows - 1;
    while (top <= bottom && left <= right)
    {
        // 向右
        for (int i = left; i <= right; i++)
        {
            cout << arr[top][i] << " ";
        }
        top++;
        // 向下
        for (int i = top; i <= bottom; i++)
        {
            cout << arr[i][right] << " ";
        }
        right--;
        // 向左
        for (int i = right; i >= left; i--)
        {
            cout << arr[bottom][i] << " ";
        }
        bottom--;
        // 向上
        for (int i = bottom; i >= top; i--)
        {
            cout << arr[i][left] << " ";
        }
        left++;
    }
    cout<<endl;
}
int main()
{
    int rows = 5, cols = 5;

    int **arr = new int *[rows];
    for (int i = 0; i < rows; i++)
    {
        arr[i] = new int[cols];
    }
    fillArray(arr, rows, cols);
    cout<<"排序前数组结果: \n";
    printArray(arr, rows, cols);
    for (int i = 0; i < rows; i++)
    {
        sortArray(arr[i], cols);
    }

    cout << "排序后数组结果： \n";
    printArray(arr, rows, cols);

    cout << "螺旋打印结果： \n";
    spiralPrint(arr, rows, cols);

    for (int i = 0; i < rows; ++i)
    {
        delete[] arr[i];
    }
    delete[] arr;
    return 0;
}
